﻿using System;
using System.Data.Services.Client;
using System.Linq;
using YourWorld.Common.GreenWorldEntitiesService;
using BusinessObjects;
using System.Collections.Generic;

namespace MainGameModule.Models
{
    public class BadgesRepository
    {
        public event Action<DataServiceCollection<Badge>> BadgesLoaded;
        public event Action<DataServiceCollection<BadgeType>> BadgeTypesLoaded;

        private const string ServiceUrl = @"http://48de1d712dd7438f8e308ea3b1e69aae.cloudapp.net/GreenWorldDataService.svc";
        private GreenWorldEntities context;

        private static BadgesRepository instance;

        public static BadgesRepository Instance
        {
            get
            {
                if ( BadgesRepository.instance == null )
                {
                    BadgesRepository.instance = new BadgesRepository();
                }

                return BadgesRepository.instance;
            }
        }

        public List<Badge> Badges
        {
            get;
            private set;
        }

        public List<BadgeType> BadgeTypes
        {
            get;
            private set;
        }

        public BadgesRepository()
        {
            this.context = new GreenWorldEntities( new Uri( ServiceUrl, UriKind.RelativeOrAbsolute ) );
        }

        public UserBadge GetBadge( int id )
        {
            Badge badge = this.Badges.Where( x => x.ID == id ).FirstOrDefault();

            UserBadge userBadge = new UserBadge()
            {
                Id = badge.ID,
                Image = badge.Image,
                TargetItems = badge.MinimumItems,
                Description = badge.Description,
                TypeId = badge.Type,
                Title = badge.Title,
                Requires = badge.Requires
            };

            return userBadge;
        }

        public void Initialize()
        {
            this.LoadBadgesAsync();
            this.LoadBadgeTypesAsync();
        }

        public void LoadBadgesAsync()
        {
            //var query = from b in this.context.Badges
            //            select new Badge
            //            {
            //                ID = b.ID,
            //                Type = b.Type,
            //                Description = b.Description,
            //                MinimumItems = b.MinimumItems,
            //                Image = b.Image,
            //                Title = b.Title,
            //                Requires = b.Requires
            //            };

            //this.Badges = new DataServiceCollection<Badge>();
            //this.Badges.LoadCompleted += new EventHandler<LoadCompletedEventArgs>( OnBadgesLoaded );
            //this.Badges.LoadAsync( query );

            var badgeObjects = ( from r in context.Badges select r ) as DataServiceQuery<Badge>;
            badgeObjects.BeginExecute( new AsyncCallback( this.LoadBadgesCompleted ), badgeObjects );
        }

        private void LoadBadgesCompleted( IAsyncResult state )
        {
            var badgeObjects = state.AsyncState as DataServiceQuery<Badge>;
            this.Badges = badgeObjects.EndExecute( state ).ToList();

            if ( this.BadgesLoaded != null )
                this.BadgesLoaded( null );
        }

        public void LoadBadgeTypesAsync()
        {
            //var query = from bt in this.context.BadgeTypes
            //            select new BadgeType
            //            {
            //                ID = bt.ID,
            //                Description = bt.Description,
            //                Key = bt.Key,
            //                TargetId = bt.TargetId
            //            };

            //this.BadgeTypes = new DataServiceCollection<BadgeType>();
            //this.BadgeTypes.LoadCompleted += new EventHandler<LoadCompletedEventArgs>( OnBadgeTypesLoaded );
            //this.BadgeTypes.LoadAsync( query );

            var badgeTypeObjects = ( from r in context.BadgeTypes select r ) as DataServiceQuery<BadgeType>;
            badgeTypeObjects.BeginExecute( new AsyncCallback( this.LoadBadgeTypesCompleted ), badgeTypeObjects );
        }

        private void LoadBadgeTypesCompleted( IAsyncResult state )
        {
            var badgeTypeObjects = state.AsyncState as DataServiceQuery<BadgeType>;
            this.BadgeTypes = badgeTypeObjects.EndExecute( state ).ToList();

            if ( this.BadgeTypesLoaded != null )
                this.BadgeTypesLoaded( null );
        }

        private void OnBadgesLoaded( object sender, LoadCompletedEventArgs e )
        {
            if ( this.BadgesLoaded != null )            
                this.BadgesLoaded( null );
            
        }

        private void OnBadgeTypesLoaded( object sender, LoadCompletedEventArgs e )
        {
            if ( this.BadgeTypesLoaded != null )
                this.BadgeTypesLoaded( null );
        }
    }
}